package com.ugrow.ugrowcommunity.activity.courseselection;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.ugrow.ugrowcommunity.BuildConfig;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.courseselection.adapter.CourseTchAdapter;
import com.ugrow.ugrowcommunity.activity.courseselection.data.CourseTchBean;
import com.ugrow.ugrowcommunity.activity.courseselection.data.Teachers;
import com.ugrow.ugrowcommunity.activity.setting.SetActivity;
import com.ugrow.ugrowcommunity.data.Data;
import com.ugrow.ugrowcommunity.databinding.FragmentSelectionTeacherBinding;
import com.ugrow.ugrowcommunity.util.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;


public class SelectionTeacherFragment extends BottomSheetDialogFragment {


    private FragmentSelectionTeacherBinding mBinding;
    private static List<Teachers> list;
    private FragmentActivity mContent;
    private CallBack callBack;


    public SelectionTeacherFragment(CourseTchBean courseTchBean,CallBack callBack){
        list = courseTchBean.getData().getTeachers();
        this.callBack = callBack;
    }

    public interface CallBack{
        void callBack(boolean success,int position);
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContent = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentSelectionTeacherBinding.inflate(getLayoutInflater());
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        //设置背景为透明
        window.setWindowAnimations(R.style.AnimBottom);
        window.setBackgroundDrawable(ContextCompat.getDrawable(requireContext(), android.R.color.transparent));
        //去除阴影
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.dimAmount = 0.1f;
        window.setAttributes(layoutParams);

        CourseTchAdapter courseTchAdapter = new CourseTchAdapter(getActivity(), list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.courseTch.setLayoutManager(layoutManager);
        mBinding.courseTch.setAdapter(courseTchAdapter);

        mBinding.yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int selectTch = courseTchAdapter.getSelectTch();
                if (selectTch == -1){
                    Toast.makeText(mContent,"请先选择授课老师！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Teachers teachers = list.get(selectTch);

                HashMap<String, Object> map = new HashMap<>();
                map.put("stuNum", Data.StuNum) ;
                map.put("tchNum",teachers.getTchNum());
                map.put("cId",teachers.getCourse().getCId());
                HttpUtil.httpPost(BuildConfig.SAVE_COURSE, map, HttpUtil.ContentType.JSON, new HttpUtil.CallBack() {
                    @Override
                    public void success(HttpUtil.Result result) {
                        View dialog = View.inflate(mContent,R.layout.dialog_sure,null);
                        Button btn_yes = dialog.findViewById(R.id.dialog_yes);
                        Button btn_cancel = dialog.findViewById(R.id.dialog_cancel);
                        AlertDialog alertDialog = new AlertDialog.Builder(mContent)
                                .setView(dialog)
                                .show();
                        btn_yes.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(mContent, "选择成功!", Toast.LENGTH_SHORT).show();
                                callBack.callBack(true,courseTchAdapter.getSelectTch());
                                alertDialog.dismiss();
                            }
                        });
                        btn_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                alertDialog.dismiss();
                            }
                        });
                    }

                    @Override
                    public void failed(IOException e) {
                        e.printStackTrace();
                        Toast.makeText(mContent, e.getMessage(), Toast.LENGTH_SHORT).show();
                        callBack.callBack(false,courseTchAdapter.getSelectTch());
                    }
                });
                dismiss();

            }
        });
        mBinding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });


    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}